package com.onLineCar.file.service;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.GetObjectRequest;
import com.onLineCar.file.config.OssConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;

/**
 * @program: OnLineCar
 * @description:
 * @author: godGuang
 * @create: 2023-12-25 16:25
 **/
@Service
public class ISysFileDownServiceImpl implements ISysFileDownService {
    @Autowired
    private OssConfig ossConfig;
    @Autowired
    private OSS ossClient;
    @Override
    public void downloadFile(String ossFilePath) {
        String fileName="";
        // 寻找最后一个斜杠的位置
        int lastSlashIndex = ossFilePath.lastIndexOf("/");
        // 如果找到了斜杠，取斜杠后的部分作为文件名
        if (lastSlashIndex != -1 && lastSlashIndex < ossFilePath.length() - 1) {
            fileName=ossFilePath.substring(lastSlashIndex + 1);
        } else {
            // 如果找不到斜杠，直接返回整个路径作为文件名
            fileName=ossFilePath;
        }
        try {
            ossClient.getObject(new GetObjectRequest(ossConfig.getBucketName(), ossFilePath), new File("C:\\Users\\41235\\Downloads\\"+fileName));
        } catch (OSSException e) {
            // 处理 OSS 异常
            e.printStackTrace();
        } catch (ClientException e) {
            // 处理 OSS 客户端异常
            e.printStackTrace();
        }
    }
}

